/*
 * GRoomsWorkerWaiter.java
 *
 * Created on August 26, 2007, 5:14 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.psu.bd.math.grooms;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.ExecutionException;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author jfp149
 */
class GRoomsWorkerWaiter implements PropertyChangeListener {
    private final JDialog dialog;
    
    private final GRoomsWorker worker;
    
    public GRoomsWorkerWaiter(JDialog dialog, GRoomsWorker worker) {
        this.dialog = dialog;
        this.worker = worker;
    }
    
    public void propertyChange(PropertyChangeEvent event) {
        if (event.getPropertyName().equals("state")) {
            
            if (SwingWorker.StateValue.DONE == event.getNewValue()) {
                dialog.setVisible(false);
                dialog.dispose();
                
                
                
                JFrame childFrame = new JFrame("GroomsAnalyzer");
                GRoomsAnalyzer contentPane;
                try {
                    if (worker.isCancelled())
                        return;
                    contentPane = new GRoomsAnalyzer(
                            worker.getGoRoom(), worker.get());
                    
                    contentPane.setOpaque(true);
                    childFrame.setContentPane(contentPane);
                    
                    //Display the window.
                    childFrame.pack();
                    childFrame.setVisible(true);
                } catch (InterruptedException ex) {
                    
                } catch (ExecutionException ex) {
                    ex.printStackTrace();
                    ex.getCause().printStackTrace();
                }
                
                
                
                
                
                
            }
        }else if (event.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)){
            
            worker.cancel(true);
            dialog.setVisible(false);
            dialog.dispose();
            
            
        }
    }
}